<?php

namespace app\common\business;

use app\common\lib\Arr;
use app\common\lib\Time;
use app\common\model\mysql\SystemCategory;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\Exception;
use think\Model;

class Statistics
{
    /**
     * 获取顶部统计
     * @param $departmentId
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getTopNum($departmentId){
        $departmentBis = new Department();
        $workBis = new Work();
        $userBis = new User();
        $feedBackBis = new FeedBack();

        $conditionDep = [];
        $conditionWork = [];
        $conditionFeedBack = [];
        if($departmentId != 0){
            $conditionDep[] = ['id','=',$departmentId];
            $conditionWork[] = ['department_id','=',$departmentId];
            $conditionFeedBack[] = ['id','=',-1];//不显示意见反馈
        }

        //全部
        $departmentNums = $departmentBis->getListByWhere($conditionDep,0,0,'id');
        $workNums = $workBis->getListByWhere($conditionWork,0,0,'id');
        $userNums = $userBis->getListByWhere([],0,0,'id');
        $adviseNums = $feedBackBis->getListByWhere($conditionFeedBack,0,0,'id');

        //当月新增时间条件
        $month_start = strtotime(date("Y-m-01"));
        $month_end = strtotime("+1 month -1 seconds", $month_start);
        $map = ['create_time','between',[$month_start,$month_end]];

        $conditionDep[] = $map;
        $conditionWork[] = $map;
        $conditionFeedBack[] = $map;
        $addDepartmentNums = $departmentBis->getListByWhere($conditionDep,0,0,'id');
        $addWorkNums = $workBis->getListByWhere($conditionWork,0,0,'id');
        $addUserNums = $userBis->getListByWhere([$map],0,0,'id');
        $addAdviseNums = $feedBackBis->getListByWhere($conditionFeedBack,0,0,'id');

        $result = [
            'departmentNums'=>count($departmentNums),
            'addDepartmentNums'=>count($addDepartmentNums),
            'workNums'=>count($workNums),
            'addWorkNums'=>count($addWorkNums),
            'userNums'=>count($userNums),
            'addUserNums'=>count($addUserNums),
            'adviseNums'=>count($adviseNums),
            'addAdviseNums'=>count($addAdviseNums),
        ];
        return $result;
    }

    /**
     * 零活饼图相关
     * @param $departmentId
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getWorkPie($departmentId){
        $workBis = new Work();
        $workTypeBis = new WorkType();

        $valuationArr = [];
        $processStatusArr = [];
        $typeArr = [];
        $valuation = config('work.valuation');
        $processStatus = config('work.process_status');
        $type = $workTypeBis->categoryDictionaries();
        foreach ($valuation as $k=>$v){
            $valuationArr[$k]['name'] = $v;
            $valuationArr[$k]['value'] = 0;
        }
        foreach ($processStatus as $k=>$v){
            $processStatusArr[$k]['name'] = $v;
            $processStatusArr[$k]['value'] = 0;
        }
        foreach ($type as $k=>$v){
            $typeArr[$k]['name'] = $v;
            $typeArr[$k]['value'] = 0;
        }

        $where = [];
        $departmentId != 0 && $where[] = ['department_id','=',$departmentId];

        $workList = $workBis->getListByWhere($where,0,0,'id,valuation,process_status,type');
        if (!empty($workList)){
            foreach ($workList as $k=>$v){
                isset($valuationArr[$v['valuation']]) && $valuationArr[$v['valuation']]['value']++;
                isset($processStatusArr[$v['process_status']]) && $processStatusArr[$v['process_status']]['value']++;
                isset($typeArr[$v['type']]) && $typeArr[$v['type']]['value']++;
            }
        }

        //只显示有记录的工种类型
        foreach ($typeArr as $k=>$v){
            if ($v['value'] == 0){
                unset($typeArr[$k]);
            }
        }

        $result = [
            'valuationList'=>array_values($valuationArr),
            'processStatusList'=>array_values($processStatusArr),
            'typeList'=>array_values($typeArr),
        ];
        return $result;
    }

    /**
     * 零活柱状图
     * @param $departmentId
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getWorkLine($departmentId){
        $workBis = new Work();

        $numData = [];
        $typeData = [];

        //近十五天
        $dateArr = Time::nearDate();
        foreach ($dateArr as $k=>$v){
            $numData[$v] = 0;
            $typeData[$v] = [0,0,0]; //0短期工 1钟点工 2计件工
        }
        //dump($dateArr,$numData,$typeData);exit;

        $where = [];
        $departmentId != 0 && $where[] = ['department_id','=',$departmentId];

        $workList = $workBis->getListByWhere($where,0,0,'id,valuation,create_time');
        if (!empty($workList)){
            foreach ($workList as $k=>$v){
                $date = explode(' ',$v['create_time'])[0];

                isset($numData[$date]) && $numData[$date]++;
                if(isset($typeData[$date])){
                    $typeData[$date][$v['valuation']-1]++;
                }
            }

            foreach ($numData as $k=>$v){
                $date_md = substr($k,5);
                $numData[$date_md] = $v;
                unset($numData[$k]);
            }
            foreach ($typeData as $k=>$v){
                $date_md = substr($k,5);
                $typeData[$date_md] = $v;
                unset($typeData[$k]);
            }
        }

        $result = [
            'typeData'=>$typeData,
            'numData'=>$numData,
        ];
        return $result;
    }

    /**
     * 用户饼图相关
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getUserPie(){

        $userBis = new User();
        $userInfoBis = new UserInfo();
        $systemCategoryBis = new Category();
        $workTypeBis = new WorkType();

        $isIntentionArr = [
            1=>['name'=>'求职者','value'=>0],
            2=>['name'=>'非求职者','value'=>0],
        ];
        $sexArr = [
            1=>['name'=>'男','value'=>0],
            2=>['name'=>'女','value'=>0],
        ];
        $intentionArr = [];
        $educationArr = [];
        $freeArr = [];
        $experienceArr = [];

        $intention = $workTypeBis->categoryDictionaries();
        $education = $systemCategoryBis->getCategoryByName('学历');
        $free = $systemCategoryBis->getCategoryByName('空闲时间');
        $experience = $systemCategoryBis->getCategoryByName('经验');

        foreach ($intention as $k=>$v){
            $intentionArr[$k]['name'] = $v;
            $intentionArr[$k]['value'] = 0;
        }
        foreach ($education as $k=>$v){
            $educationArr[$v['id']]['name'] = $v['name'];
            $educationArr[$v['id']]['value'] = 0;
        }
        foreach ($free as $k=>$v){
            $freeArr[$v['id']]['name'] = $v['name'];
            $freeArr[$v['id']]['value'] = 0;
        }
        foreach ($experience as $k=>$v){
            $experienceArr[$v['id']]['name'] = $v['name'];
            $experienceArr[$v['id']]['value'] = 0;
        }

        $userList = $userBis->getListByWhere([],0,0,'id,sex');
        if (!empty($userList)){
            $userIdArr = array_column($userList,'id');

            $mapUserInfo = [
                ['user_id','in',$userIdArr]
            ];
            $userInfoList = $userInfoBis->getListByWhere($mapUserInfo,0,0,'id,user_id,education,experience,intention,free');
            $userInfoArr = [];
            if (!empty($userInfoList)){
                $userInfoArr = Arr::getKeyValueByTwoArray($userInfoList,'','user_id',2);
            }

            foreach ($userList as $k=>$v){
                if (isset($userInfoArr[$v['id']])){
                    $userInfo = $userInfoArr[$v['id']];

                    $userInfo['intention'] != 0 ? $isIntentionArr[1]['value']++ : $isIntentionArr[2]['value']++;
                    isset($intentionArr[$userInfo['intention']]) && $intentionArr[$userInfo['intention']]['value']++;
                    isset($educationArr[$userInfo['education']]) && $educationArr[$userInfo['education']]['value']++;
                    isset($freeArr[$userInfo['free']]) && $freeArr[$userInfo['free']]['value']++;
                    isset($experienceArr[$userInfo['experience']]) && $experienceArr[$userInfo['experience']]['value']++;
                }

                isset($sexArr[$v['sex']]) && $sexArr[$v['sex']]['value']++;
            }
        }

        //只显示有记录的工种类型
        foreach ($intentionArr as $k=>$v){
            if ($v['value'] == 0){
                unset($intentionArr[$k]);
            }
        }

        $result = [
            'isIntentionList'=>array_values($isIntentionArr),
            'sexList'=>array_values($sexArr),
            'intentionList'=>array_values($intentionArr),
            'educationList'=>array_values($educationArr),
            'freeList'=>array_values($freeArr),
            'experienceList'=>array_values($experienceArr),
        ];
        return $result;
    }

    /**
     * 获取排名表格
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getTable(){
        $departmentBis = new Department();
        $userBis = new User();
        $commentBis = new Comments();

        $departmentList = $departmentBis->getListByWhere([],0,0,'id,name,employer_score,workers_score');
        if (!empty($departmentList)){
            foreach ($departmentList as $k=>$v){
                $departmentList[$k]['total_score'] = round($v['employer_score'] + $v['workers_score']);
            }
            $departmentList = Arr::twoArraySortByField($departmentList,'total_score','desc');
            $departmentList = array_splice($departmentList,0,10);

            $order = 1;
            foreach ($departmentList as $k=>$v){
                $departmentList[$k]['order'] = $order;
                $order++;
            }
        }

        $userList = $userBis->getListByWhere([],0,0,'id,name,sex,phone_number');
        if (!empty($userList)){
            $userIdArr = array_column($userList,'id');

            $mapComment = [
                ['type','=',1],
                ['associated_id','in',$userIdArr]
            ];
            $commentList = $commentBis->getListByWhere($mapComment,0,0,'id,type,associated_id,score');
            $commentArr = [];
            if (!empty($commentList)){
                foreach ($commentList as $k=>$v){
                    $commentArr[$v['associated_id']] = isset($commentArr[$v['associated_id']]) ? $commentArr[$v['associated_id']]+ $v['score'] : $v['score'];
                }
            }

            foreach ($userList as $k=>$v){
                $userList[$k]['total_score'] = isset($commentArr[$v['id']]) ? round($commentArr[$v['id']]) : 0;
            }
            $userList = Arr::twoArraySortByField($userList,'total_score','desc');
            $userList = array_splice($userList,0,10);

            $order1 = 1;
            foreach ($userList as $k=>$v){
                $userList[$k]['order'] = $order1;
                $order1++;
            }
        }

        $result = [
            'departmentList'=>$departmentList,
            'userList'=>$userList,
        ];
        return $result;
    }

    public function getWxUserTable(){
        $userBis = new User();
        $feedBackBis = new FeedBack();

        $userCount = 0;
        $authUserCount = 0;
        $feedBackCount = 0;
        $userAssociatedCount = 0;
        $manCount = 0;
        $womanCount = 0;

        $userList = $userBis->getTableList();
        if (!empty($userList['list'])){
            foreach ($userList['list'] as $k=>$v){
                $userCount++;
                $v['autherized'] == 1 && $authUserCount++;
                $v['sex'] == 1 && $manCount++;
                $v['sex'] == 2 && $womanCount++;
                if (isset($v['userAssociation'])) $userAssociatedCount++;
            }
        }

        $feedBackList = $feedBackBis->getListByWhere([],0,0,'id');
        $feedBackCount = count($feedBackList);

        $result = [
            'userCount'=>$userCount,
            'authUserCount'=>$authUserCount,
            'feedBackCount'=>$feedBackCount,
            'userAssociatedCount'=>$userAssociatedCount,
            'manCount'=>$manCount,
            'womanCount'=>$womanCount,
        ];
        return $result;
    }
}
